<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UFT-8">
<title>组织机构</title>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<script src="../../hplus/js/jquery.min.js"></script>
<link rel="stylesheet" href="../../ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="../../ztree/js/jquery.ztree.core.js"></script>
<link rel="stylesheet" type="text/css" href="../../easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="../../easyui/themes/icon.css">
<script type="text/javascript" src="../../easyui/jquery.easyui.min.js?1=1" charset="gbk"></script>
<style type="text/css">
	.tabs
	{
		font-family:微软雅黑;
		font-size:14px;
		height: 36px;
		background-color: rgb(224,236,255);
		width: 100%;
	}
	.tab-btn
	{
		float:left;
		margin-top:5px;
		line-height:30px;
		border: 1px solid rgb(186,219,234);
		margin-left:5px;
		height: 30px;
		padding-right: 5px;
		padding-left: 25px;
		border-top-left-radius:0.3em;
		border-top-right-radius:0.3em;
		cursor: pointer;
		color: rgb(26,110,187);
	}
	.tab-btn-active
	{
		border-bottom: 1px solid white;
	}
	
	.form-edit
	{
		border-collapse:collapse;
		width: 100%;
	}
	.form-edit td
	{
		background-color: rgb(241,241,239);
		border: 1px solid rgb(228,226,223);
		padding-left: 5px;
	}
	.btn
	{
		padding-left: 5px;
		padding-right: 5px;
		height: 30px;
		line-height: 30px;
		text-align: center;
		border-radius:5px;
		color: white;
		background-color: rgb(0,197,228);
		cursor: pointer;
		float: left;
		margin:5px 5px 5px 5px;
		font-weight: bold;
	}
	.btn:hover
	{
		background-color: rgb(0,170,228);
	}
	.icon-dandy{
		background:url('icons/1_close.png') no-repeat;
	}
	
</style>
</head>
<body class="easyui-layout">
	<div data-options="region:'west',split:true,title:'组织机构'" style="width:180px;overflow: hidden;">
		<ul id="orgTree" class="ztree" ></ul>
	</div>
	<div data-options="region:'center'" style="overflow-x:hidden;overflow-y:hidden;padding: 3px;">
		<div id="tt" class="easyui-tabs" style="width: 100%; height: 100%;overflow-x:hidden;overflow-y:hidden;">
			<div title="组织机构新建" style="padding: 3px">
				<form id="orgForm">
					<table class="form-edit">
						<tr>
							<td width="10%;">
								机构编号：
							</td>
							<td width="40%">
								<input id="orgCode" class="easyui-textbox" style="width:200px;height:32px">
							</td>
							<td width="10%;">
								<font color="red">*</font>机构名称：
							</td>
							<td width="40%">
								<input id="orgName" class="easyui-textbox" data-options="required:true" style="width:200px;height:32px;">
							</td>
						</tr>
						<tr>
							<td width="10%;">
								<font color="red">*</font>机构简称：
							</td>
							<td width="40%">
								<input id="orgShortName" data-options="required:true" class="easyui-textbox" data-options="prompt:'组织机构列表展示简称'" style="width:200px;height:32px">
							</td>
							<td width="10%;">
								归属机构名称：
							</td>
							<td width="40%">
								<input id="parentOrgName" data-options="readonly:true" class="easyui-textbox"  style="width:200px;height:32px">
							</td>
						</tr>
						<tr>
							<td width="10%;">
								启用日期：
							</td>
							<td width="40%">
								<input id="enableDate" class="easyui-datebox" style="width:200px;height: 32px;">
							</td>
							<td colspan="2">
							</td>
						</tr>
						<tr>
							<td width="10%">
								机构职责描述：
							</td>
							<td colspan="3">
								<input id="orgDuty" data-options="multiline:true" class="easyui-textbox" style="width:800px;height:64px">
							</td>
						</tr>
						<tr style="display: none;">
							<td>归属机构ID：</td>
							<td>							
								<input id="parentId" class="easyui-textbox"  style="width:200px;height:32px">
							</td>
						</tr>
						<tr>
							<td colspan="4">
								<div>
									<div id="save" class="btn" style="width: 50px;">提交</div>
									<div id="reset" class="btn" style="width: 50px;">重置</div>
								</div>
							</td>
						</tr>
					</table>
				</form>
			</div>
			<div title="组织机构管理" style="padding: 3px;overflow: hidden;">
				<div class="easyui-layout" data-options="fit:true">
					<div data-options="region:'north',border:false" style="height:56px">
						<table class="form-edit">
							<tr>
								<td width="10%;">
									归属机构名称：
								</td>
								<td width="40%">
									<input id="i_parentOrgName" class="easyui-textbox" style="width:200px;height:22px">
								</td>
								<td width="10%;">
									机构名称：
								</td>
								<td width="40%">
									<input id="i_orgName" class="easyui-textbox" style="width:200px;height:22px">
								</td>
							</tr>
							<tr>
								<td width="10%;">
									机构编码：
								</td>
								<td width="40%">
									<input id="i_orgCode" class="easyui-textbox" style="width:200px;height:22px">
								</td>
								<td width="10%;" colspan="2">
								</td>
							</tr>
						</table>
					</div>
					<div data-options="region:'center',border:false">
						<table id="orgGrid" class="easyui-datagrid"
								data-options="method:'get',rownumbers:true,
								toolbar:toolbar,fit:true,fitColumns:true">
							<thead>
								<tr>
									<th field="ck" checkbox="true"></th>
									<th data-options="field:'id',width:80,hidden:true">id</th>
									<th data-options="field:'parentOrgCode',width:100">归属机构编号</th>
									<th data-options="field:'parentOrgName',width:100">归属机构名称</th>
									<th data-options="field:'orgCode',width:100">机构编号</th>
									<th data-options="field:'orgName',width:100">机构名称</th>
									<th data-options="field:'orgShortName',width:100">机构简称</th>
									<th data-options="field:'orgDuty',width:100">责任描述</th>
									<th data-options="field:'enableDate',width:100">启用时间</th>
								</tr>
							</thead>
						</table>
					</div>
				</div>
			</div>
			<div title="组织机构修改" style="padding: 3px">
				<form id="updateForm">
					<table class="form-edit">
						<tr style="display: none;">
							<td>
								<input id="u_id" class="easyui-textbox" style="width:200px;height:32px">
							</td>
						</tr>
						<tr>
							<td width="10%;">
								机构编号：
							</td>
							<td width="40%">
								<input id="u_orgCode" class="easyui-textbox" style="width:200px;height:32px">
							</td>
							<td width="10%;">
								<font color="red">*</font>机构名称：
							</td>
							<td width="40%">
								<input id="u_orgName" class="easyui-textbox" data-options="required:true" style="width:200px;height:32px;">
							</td>
						</tr>
						<tr>
							<td width="10%;">
								<font color="red">*</font>机构简称：
							</td>
							<td width="40%">
								<input id="u_orgShortName" class="easyui-textbox" data-options="required:true" data-options="prompt:'组织机构列表展示简称'" style="width:200px;height:32px">
							</td>
							<td width="10%;">
								归属机构名称：
							</td>
							<td width="40%">
								<input id="u_parentOrgName" data-options="readonly:true" class="easyui-textbox"  style="width:200px;height:32px">
							</td>
						</tr>
						<tr>
							<td width="10%;">
								启用日期：
							</td>
							<td width="40%">
								<input id="u_enableDate" class="easyui-datebox" style="width:200px;height: 32px;">
							</td>
							<td colspan="2">
							</td>
						</tr>
						<tr>
							<td width="10%">
								机构职责描述：
							</td>
							<td colspan="3">
								<input id="u_orgDuty" data-options="multiline:true" class="easyui-textbox" style="width:800px;height:64px">
							</td>
						</tr>
						<tr style="display: none;">
							<td>归属机构ID：</td>
							<td>							
								<input id="u_parentId" class="easyui-textbox"  style="width:200px;height:32px">
							</td>
						</tr>
						<tr>
							<td colspan="4">
								<div>
									<div id="update" class="btn" style="width: 50px;">提交</div>
								</div>
							</td>
						</tr>
					</table>
				</form>
			</div>
			<div title="管理员授权" data-options="iconCls:'icon-dandy'"
				style="padding: 3px">
				<table id="adminGrid" class="easyui-datagrid" title="授权列表"
						data-options="method:'get',
						toolbar:toolbar2,fit:true,fitColumns:true">
					<thead>
						<tr>
							<th field="ck" checkbox="true"></th>
							<th data-options="field:'id',width:80,hidden:true">id</th>
							<th data-options="field:'account',width:100">管理员账号</th>
							<th data-options="field:'name',width:100">管理员名称</th>
							<th data-options="field:'type',width:100">类别</th>
							<th data-options="field:'status',width:100">状态</th>
						</tr>
					</thead>
				</table>	
			</div>
		</div>
	</div>
	<div id="w" class="easyui-window" title="管理员列表" data-options="iconCls:'icon-save'" closed="true" style="width:700px;height:330px;padding:5px;">
		<div class="easyui-layout" data-options="fit:true">
			<table id="adminChooseGrid" class="easyui-datagrid"
					data-options="method:'get',
					fit:true,toolbar:toolbarWin,fitColumns:true">
				<thead>
					<tr>
						<th field="ck" checkbox="true"></th>
						<th data-options="field:'id',width:80,hidden:true">id</th>
						<th data-options="field:'account',width:100">管理员账号</th>
						<th data-options="field:'name',width:100">管理员名称</th>
						<th data-options="field:'type',width:100">类别</th>
						<th data-options="field:'status',width:100">状态</th>
					</tr>
				</thead>
			</table>
		</div>
	</div>
</body>
	
<script type="text/javascript">
$(document).ready(function() {
	setDefaultDate();
	generOrgTree();
	
	$('#tt').tabs('select', "组织机构管理");
	//查询
	query();
	
});

var zTreeObj;
//zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
var setting = {
	callback:{
		onClick:zTreeOnClick
	}
};

function query(){
	$.ajax({
        type: "post",
        dataType: "json",
        url: '${ctx}/org/getOrgListWithParent',
        success: function (data) {
        	$('#orgGrid').datagrid('loadData', data);
        },
		error:function(){
		}
    });
	
}

function toUpdate(){
	var rows = $('#orgGrid').datagrid('getSelections');
	if(rows.length < 1){
		$.messager.alert('消息','请选择一条记录','info');
		return;
	}
	if(rows.length > 1){
		$.messager.alert('消息','最多选择一条记录','info');
		return;
	}
	for(var i=0; i<rows.length; i++){
		$("#u_id").textbox('setValue', rows[i].id);
		$("#u_orgCode").textbox('setValue', rows[i].orgCode);
		$("#u_orgName").textbox('setValue', rows[i].orgName);
		$("#u_orgShortName").textbox('setValue', rows[i].orgShortName);
		$("#u_parentOrgName").textbox('setValue', rows[i].parentOrgName);
		$("#u_orgDuty").textbox('setValue', rows[i].orgDuty);
		$("#u_enableDate").datebox("setValue",rows[i].enableDate);
	}
	
	$('#tt').tabs('select', "组织机构修改");
}

$("#update").on("click",function(){
	if($("#u_id").val()==null || $("#u_id").val()==""){
		$.messager.alert('消息',"请选择修改记录",'info');
		return ;
	}
	
	$.messager.confirm('确认提示', '确定修改?', function(r){
		if (r){
			$.ajax({
		        type: "post",
		        dataType: "json",
		        url: '${ctx}/org/updateOrg',
		        data:{
		        	id:$("#u_id").val(),
		        	orgCode:$("#u_orgCode").val(),
		        	orgName:$("#u_orgName").val(),
		        	orgShortName:$("#u_orgShortName").val(),
		        	orgDuty:$("#u_orgDuty").val(),
		        	enableDate:$("#u_enableDate").datebox("getValue")
		        },
		        success: function (data) {
		        	query();
		        	generOrgTree();
		        	$.messager.alert('消息',data.msg,'info');
		        },
				error:function(){
				}
		    });
		}
	});
});

function deleteOrgs(){
	var rows = $('#orgGrid').datagrid('getSelections');
	if(rows.length < 1){
		$.messager.alert('消息','请选择一条记录','info');
	}
	
	var orgIds = [];
	for(var i=0; i<rows.length; i++){
		orgIds.push(rows[i].id);
	}
	$.ajax({
        type: "post",
        dataType: "json",
        url: '${ctx}/org/deleteOrgs',
        data:{
        	"orgIds":orgIds.join(",")
        },
        success: function (data) {
        	if(data.status=='1'){
        		query();
        		$.messager.alert('消息',data.msg,'info');
        	}else{
        		$.messager.alert('消息',data.msg,'error');
        	}
        	
        },
		error:function(){
		}
    });
}

function setDefaultDate(){
	var date = new Date();
	var y = date.getFullYear();
	var m = date.getMonth()+1;
	var d = date.getDate();
	$("#enableDate").datebox("setValue",m+'/'+d+'/'+y);
}

function zTreeOnClick(event, treeId, treeNode){
	$('#orgForm').form('clear');
	
	$("#parentId").textbox("setValue", treeNode.id);
	$("#parentOrgName").textbox("setValue", treeNode.name);
	setDefaultDate();
	
	queryUserAuthList(treeNode.id);
}

$("#save").on("click",function(){
	$.messager.confirm('确认提示', '确定保存?', function(r){
		if (r){
			$.ajax({
		        type: "post",
		        dataType: "json",
		        url: '${ctx}/org/addOrg',
		        data:{
		        	orgCode:$("#orgCode").val(),
		        	orgName:$("#orgName").val(),
		        	orgShortName:$("#orgShortName").val(),
		        	parentId:$("#parentId").val(),
		        	orgDuty:$("#orgDuty").val(),
		        	enableDate:$("#enableDate").datebox("getValue")
		        },
		        success: function (data) {
		        	generOrgTree();
		        	$('#orgForm').form('clear');
		        	setDefaultDate();
		        },
				error:function(){
				}
		    });
		}
	});
});

$("#reset").on("click",function(){
	var parentId = $("#parentId").val();
	var parentOrgName = $("#parentOrgName").val();
	$('#orgForm').form('clear');
	$("#parentId").textbox("setValue", parentId);
	$("#parentOrgName").textbox("setValue", parentOrgName);
	
	setDefaultDate();
});


function queryUserAuthList(orgId){
	$.ajax({
        type: "post",
        dataType: "json",
        url: '${ctx}/sysUser/getUserAuthList',
        data:{
        	orgId:orgId
        },
        success: function (data) {
        	$('#adminGrid').datagrid('loadData', data);
        },
		error:function(){
		}
    });
	
}

function addAuth(){
	var nodes = zTreeObj.getSelectedNodes();
	if(nodes.length < 1){
		$.messager.alert('消息','请选择组织机构','info');
		return ;
	}
	$.ajax({
        type: "post",
        dataType: "json",
        url: '${ctx}/sysUser/getUserListNotAuth',
        data:{
        },
        success: function (data) {
        	$('#adminChooseGrid').datagrid('loadData', data);
        },
		error:function(){
		}
    });
	$('#w').window('open');
}


//生成组织机构树
function generOrgTree(){
	$.ajax({
        type: "post",
        dataType: "json",
        url: '${ctx}/org/getOrgTree',
        success: function (data) {
        	zTreeObj = $.fn.zTree.init($("#orgTree"), setting, data);
        },
		error:function(){
		}
    });
}

function addUserAuth(){
	var nodes = zTreeObj.getSelectedNodes();
	if(nodes.length < 1){
		$.messager.alert('消息','请选择组织机构','info');
		return ;
	}
	
	var rows = $('#adminChooseGrid').datagrid('getSelections');
	if(rows.length < 1){
		$.messager.alert('消息','请选择一条记录','info');
		return ;
	}
	
	var ids = new Array();
	for(var i=0; i<rows.length; i++){
		ids.push(rows[i].id);
	}
	
	$.messager.confirm('确认提示', '确定保存?', function(r){
		if (r){
			$.ajax({
		        type: "post",
		        dataType: "json",
		        url: '${ctx}/sysUser/addUserAuth',
		        data:{
		        	orgId:nodes[0].id,
		        	userIds:ids.join(",")
		        },
		        success: function (data) {
		        	$.messager.alert('消息',data.msg,'info');
		        	
		        	queryUserAuthList(nodes[0].id);
		        	
		        	$('#w').window('close');
		        },
				error:function(){
				}
		    });
		}
	});
	
}

function deleteUserAuth(){
	var nodes = zTreeObj.getSelectedNodes();
	if(nodes.length < 1){
		$.messager.alert('消息','请选择组织机构','info');
		return ;
	}
	
	var rows = $('#adminGrid').datagrid('getSelections');
	if(rows.length < 1){
		$.messager.alert('消息','请选择一条记录','info');
		return ;
	}
	
	var ids = new Array();
	for(var i=0; i<rows.length; i++){
		ids.push(rows[i].id);
	}
	
	$.messager.confirm('确认提示', '确定删除?', function(r){
		if (r){
			$.ajax({
		        type: "post",
		        dataType: "json",
		        url: '${ctx}/sysUser/deleteUserAuth',
		        data:{
		        	orgId:nodes[0].id,
		        	userIds:ids.join(",")
		        },
		        success: function (data) {
		        	$.messager.alert('消息',data.msg,'info');
		        	
		        	queryUserAuthList(nodes[0].id);
		        	
		        },
				error:function(){
				}
		    });
		}
	});
}


var toolbar = [{
	text:'查询',
	iconCls:'icon-search',
	handler:function(){
		query();
	}
},'-',{
	text:'修改',
	iconCls:'icon-edit',
	handler:function(){
		toUpdate();
	}
},'-',{
	text:'删除',
	iconCls:'icon-remove',
	handler:function(){
		deleteOrgs();
	}
}];

var toolbar2 = [{
	text:'添加',
	iconCls:'icon-add',
	handler:function(){
		addAuth();
	}
},'-',{
	text:'删除',
	iconCls:'icon-remove',
	handler:function(){
		deleteUserAuth();
	}
}];

var toolbarWin = [{
	text:'确定',
	iconCls:'icon-add',
	handler:function(){
		addUserAuth();
	}
},'-'];


</script>
</html>
